﻿
using System;
using System.IO;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using Bankeren.Engine;
namespace BankerenClient.Converters
{
    public class AvatarToBitmapImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Avatar avatar = (Avatar)value;
            if (avatar.ImageBytes != null)
                using (MemoryStream stream = new MemoryStream(avatar.ImageBytes))
                {
                    BitmapImage image = new BitmapImage();
                    image.BeginInit();
                    image.StreamSource = stream;
                    image.EndInit();
                    return image;
                }
            else
            {
                Stream stream = this.GetType().Assembly.GetManifestResourceStream("BankerenClient.Resources.Avatars." + avatar.Name + ".png");
                BitmapImage image = new BitmapImage();
                image.BeginInit();
                image.StreamSource = stream;
                image.EndInit();
                return image;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}
